import {
    createRouter,
    createWebHistory
} from 'vue-router'
import UserManagement from '../views/UserManagement.vue'
import Login from '../login.vue'
import UserRootManage from '../views/UserRootManage.vue'
import TagManagement from '../views/TagManagement.vue'
import ArticleViewsManagement from '../views/ArticleViewsManagement.vue'
import ArticlesManagement from '../views/ArticlesManagement.vue'
import ArticlesLikeManagement from '../views/ArticlesLikeManagement.vue'
import ArticlesStarManagement from '../views/ArticlesStarManagement.vue'
import ArticlesReplyManagement from '../views/ArticlesReplyManagement.vue'
import ArticlesNextReply from '../views/ArticlesNextReply.vue'
import UserFollowsManagement from '../views/UserFollowsManagement.vue'
import NoticeManagement from '../views/NoticeManagement.vue'

const routes = [
    {
        path: '/usermanagement',
        name: 'usermanagement',
        component: UserManagement,
        meta: {
            title: '用户管理',
            icon: 'user'
        }
    },
    {
        path: '/userrootmanage',
        name: 'userrootmanage',
        component: UserRootManage,
        meta: {
            title: '权限管理',
            icon: 'user'
        }
    },
    {
        path: '/tagmanagement',
        name: 'TagManagement',
        component: TagManagement,
        meta: {
            title: '标签管理', 
            icon: 'tag' 
        }
    },
    {
        path: '/articleviewsmanagement',
        name: 'ArticleViewsManagement',
        component: ArticleViewsManagement,
        meta: { title: '文章浏览记录' } // 面包屑标题
    },
    {
        path: '/articlesmanagement',
        name: 'ArticlesManagement',
        component: ArticlesManagement,
        meta: { title: '文章列表' }
    },
    {
        path: '/articleslikemanagement',
        name: 'ArticlesLikeManagement',
        component: ArticlesLikeManagement,
        meta: { title: '文章点赞列表' }
    },
    {
        path: '/articlesstarmanagement',
        name: 'ArticlesStarManagement',
        component: ArticlesStarManagement,
        meta: { title: '文章收藏列表' }
    },
    {
        path: '/articlesreplymanagement',
        name: 'ArticlesReplyManagement',
        component: ArticlesReplyManagement,
        meta: { title: '文章回复列表' }
    },
    {
        path: '/articlesnextreply',
        name: 'ArticlesNextReply',
        component: ArticlesNextReply,
        meta: { title: '楼中楼列表' }
    },
	{
	  path: '/userfollowsmanagement',
	  name: 'UserFollowsManagement',
	  component: UserFollowsManagement,
	  meta: { title: '用户关注管理' }
	},
	{
	  path: '/noticemanagement',
	  name: 'NoticeManagement',
	  component: NoticeManagement,
	  meta: { title: '公告栏管理' }
	},
    {
        path: '/',
        name: 'login',
        component: Login,
        meta: {
            title: '登录',
            icon: 'user'
        }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    // 如果目标路径不是登录页面
    if (to.fullPath !== "/") {
        // 检查 localStorage 中是否存在用户名和 token
        if (localStorage.getItem("uname") === null || localStorage.getItem("token") === null) {
            // 如果不存在，则重定向到登录页面
            next("/")
        } else {
            // 如果存在，则继续访问目标页面
            next()
        }
    } else {
        // 如果目标路径是登录页面，则直接访问
        next()
    }
})

export default router;